package com.global.college;

import android.text.TextUtils;

import com.global.college.constants.Constant;
import com.global.college.constants.PreKey;
import com.global.college.security.CryptionUtil;
import com.global.college.ui.bean.UserDataModel;
import com.global.college.utils.PrefUtils;

/**
 * @author: xjh
 * @datetime: 2021/9/24
 */
public class UserData {
    private static volatile UserData userData;


    private String Token;
    private String phone;
    private UserDataModel userModel;

    private UserData() {
    }

    public static UserData getInstance() {
        if (null == userData) {
            synchronized (UserData.class) {
                if (null == userData) {
                    userData = new UserData();
                }
            }
        }
        return userData;
    }
    public void setToken(String token) {
        Token = token;
        PrefUtils.setString(Constant.User.TOKEN, token);
    }

    public String getToken() {
        if (Token != null) {
            return Token;
        }
        return PrefUtils.getString(Constant.User.TOKEN, "");
    }
    public void setPhone(String phone) {
        phone = phone;
        PrefUtils.setString(Constant.User.PHONE, phone);
    }

    public String getPhone() {
        if (phone != null) {
            return phone;
        }
        return PrefUtils.getString(Constant.User.PHONE, "");
    }

    public String getUserId() {
        return PrefUtils.getString(Constant.User.UID, "");
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    public UserDataModel getUserModel() {
        if (null != userModel) {
            return userModel;
        }
        //判断本地用户json数据是否存在(这一步判断也是又必要的，user在缓存有可能被free掉，所以)
        String userBase64 = PrefUtils.getString((PreKey.LoginMessage), "");
        if (!TextUtils.isEmpty(userBase64)) {
            Object object = CryptionUtil.base64ToObject(userBase64);
            UserDataModel temp = null;
            if (null != object && object instanceof UserDataModel) {
                temp = (UserDataModel) object;
            }
            if (null != temp) {
                //登录成功
                userModel = temp;
            }
        }
        return userModel;
    }

    public void setUserModel(UserDataModel mUserModel) {
        if (mUserModel == null) {
            return;
        }
        //将user保存到本地
        String userBase64 = CryptionUtil.objectToBase64(mUserModel);
        PrefUtils.setString(PreKey.LoginMessage, userBase64);
        this.userModel = mUserModel;
        PrefUtils.setString(Constant.User.PHONE, mUserModel.getUserName());
        PrefUtils.setString(Constant.User.UID, mUserModel.getUserId());
        PrefUtils.setString(Constant.User.SEX, mUserModel.getSex());
        PrefUtils.setString(Constant.User.HEADING, mUserModel.getAvatar());
        PrefUtils.setString(Constant.User.NICKNAME, mUserModel.getNickName());
        PrefUtils.setString(Constant.User.EMAIL, mUserModel.getEmail());
        PrefUtils.setString(Constant.User.DELFLAG, mUserModel.getDelFlag());
        PrefUtils.setString(Constant.User.IS_CHECK_AGREEMENT, mUserModel.getIsCheckAgreement());
        PrefUtils.setString(Constant.User.SOURCE, mUserModel.getSource());
        PrefUtils.setString(Constant.User.STATUS, mUserModel.getStatus());
        PrefUtils.setString(Constant.User.USER_TYPE, mUserModel.getUserType());
        PrefUtils.setString(Constant.User.PASSWord, mUserModel.getPassword());
    }

    /**
     * 退出登录
     */
    public void logoutClearData() {
        //清空缓存
        userModel = null;
        Token = null;
       // 清空用户名，密码
        PrefUtils.removeValue(PreKey.LoginMessage);
    }

    /**
     * 清除所有数据
     */
    public void ClearAllCachData() {
        logoutClearData();
        //长久存储用户信息
        PrefUtils.removeValue(Constant.User.TOKEN);
        PrefUtils.removeValue(Constant.User.PHONE);
        PrefUtils.removeValue(Constant.User.UID);
        PrefUtils.removeValue(Constant.User.SEX);
        PrefUtils.removeValue(Constant.User.HEADING);
        PrefUtils.removeValue(Constant.User.NICKNAME);
        PrefUtils.removeValue(Constant.User.EMAIL);
        PrefUtils.removeValue(Constant.User.DELFLAG);
        PrefUtils.removeValue(Constant.User.IS_CHECK_AGREEMENT);
        PrefUtils.removeValue(Constant.User.SOURCE);
        PrefUtils.removeValue(Constant.User.STATUS);
        PrefUtils.removeValue(Constant.User.USER_TYPE);
        PrefUtils.removeValue(Constant.User.PASSWord);
    }
}
